package com.small.common.config.admin.impl;

import com.small.common.base.BaseConstants;
import com.small.common.config.admin.IAdminInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 作者: fenny
 * 邮箱: 893146555@qq.com
 * 时间: 2017/10/17 17:04
 * 描述: 从session中获取管理员信息
 */
public class AdminSessionImpl implements IAdminInfo {

    /**
     * @param request req
     * @return 当前用户的ID
     */
    @Override
    public long getId(HttpServletRequest request) {
        Object value = request.getSession().getAttribute(BaseConstants.KEY_ID);
        return value == null ? 0 : (long) value;
    }

    /**
     * @param request req
     * @return 当前用户账号
     */
    @Override
    public String getAccount(HttpServletRequest request) {
        Object value = request.getSession().getAttribute(BaseConstants.KEY_ACCOUNT);
        return value == null ? "" : value.toString();
    }

    @Override
    public String getName(HttpServletRequest request) {
        Object value = request.getSession().getAttribute(BaseConstants.KEY_NAME);
        return value == null ? "" : value.toString();
    }

    @Override
    public String getToken(HttpServletRequest request) {
        Object value = request.getSession().getAttribute(BaseConstants.KEY_TOKEN);
        return value == null ? "" : value.toString();
    }

    /**
     * @param request req
     * @return 1:超级管理员
     */
    @Override
    public int getIsSystem(HttpServletRequest request) {
        Object value = request.getSession().getAttribute(BaseConstants.KEY_IS_SYSTEM);
        return value == null ? 0 : (int) value;
    }

    /**
     * @param request req
     * @return 拥有的角色ID, 示例1, 2, 3, 4
     */
    @Override
    public String getRoles(HttpServletRequest request) {
        Object value = request.getSession().getAttribute(BaseConstants.KEY_ROLES);
        return value == null ? null : value.toString();
    }

    @Override
    public String getData(HttpServletRequest request) {
        Object value = request.getSession().getAttribute(BaseConstants.KEY_DATA);
        return value == null ? "" : value.toString();
    }

}
